local _G = _G
local addon, ns = ...
local profile = ns.profile
local cfg = {}
local LabelUI
local SaveVars
local noshow
local Scadawin
if not _G.LabelUI_mod then
	_G.LabelUI_mod = {}	
end
  
local Skadamod = CreateFrame("Frame", nil, UIParent)

local function UpdatePos()
	if SaveVars:Get("exprepbar|show", false) and LUI_ExpRepBar then
		Scadawin:trueSetPoint("BOTTOMRIGHT", LUI_ExpRepBar, "TOPRIGHT", 0, 3)
	elseif LUI_Panel then
		Scadawin:trueSetPoint("BOTTOMRIGHT", LUI_Panel, "TOPRIGHT", 0, 3)
	end
end

local function UpdateWindow(win)
	--win.db.barsize = 364.7926351866982
	--win.db.barmax = 7
	win.db.classicons = false
	win.db.barslocked = true
	win.db.background.color.a = 0.5
	win.db.background.color.b = 0
	win.db.background.height = 110
	win.db.y = 3
	win.db.barfont = "Aquarion"
	win.db.title.color.a = 0
	win.db.title.borderthickness = 0
	win.db.title.texture = "Minimalist"
	win.db.point = "BOTTOMRIGHT"
	win.db.barcolor.g = 0.3019607843137255
	win.db.barcolor.r = 0.3019607843137255
	win.db.barorientation = 3
	win.db.bartexture = "Minimalist"
	win.db.enablebackground = true
	win.db.barfontsize = 13
	win.db.enabletitle = false
	if not win.db.baraltcolor then win.db.baraltcolor = {} end
	win.db.baraltcolor.a = 0
	win.db.baraltcolor.g = 0.4509803921568628
	win.db.baraltcolor.r = 0.4509803921568628
	win.db.barwidth = 359
	win.db.x = -3
end
  
local function Addbutton()
		local windows = Skada:GetWindows()
		local curwin
		for i, win in ipairs(windows) do
			if win.db.name == "Skada" then
				if not win.db.labelui then
					--[[for k,v in pairs (win.db) do
						if type(v) == "table" then
							print(k, " = ", "{")
							for k,v in pairs (win.db) do
								if type(v) == "table" then
									print("  ",k, " = ", "{")
									for k,v in pairs (win.db) do
										if type(v) == "table" then
										print("    ",k, " = ", "{")
							
										print("    }")
									else
										print("    ",k, " = ", v)
									end
								end
									print("  }")
								else
									print("  ", k, " = ", v)
								end
							end
							print("}")
						else
							print(k, " = ", v)
						end
					end]]--
					UpdateWindow(win)
					win.db.labelui = true;
					Skada:ReloadSettings()
					Addbutton()
					return
				end
				curwin = win
			end
		end
		if curwin then
			local report = CreateFrame("Button", nil, curwin.bargroup)
			report:SetSize(13, 13)
			report:SetPoint("BOTTOMLEFT", 2, 2)
			report:SetFrameStrata("HIGH")
			report:SetNormalTexture("Interface\\Buttons\\UI-GuildButton-MOTD-Up")
			report:SetHighlightTexture("Interface\\Buttons\\UI-GuildButton-MOTD-Up")
			report:SetScript("OnClick", function()
				_G.Skada:OpenReportWindow(curwin)
			end)
			report:Show()
			
			curwin.bargroup:ClearAllPoints()
			curwin.bargroup.ClearAllPoints = noshow
			curwin.bargroup.trueSetPoint = curwin.bargroup.SetPoint
			curwin.bargroup.SetPoint = noshow
			Scadawin = curwin.bargroup
			Scadawin:HookScript("OnShow", function(self) 
				LabelUI.TTPoints:Set(40, Scadawin, -79, 3)
      		end)
			Scadawin:HookScript("OnHide", function(self)
				if UIParent:IsShown() then
					LabelUI.TTPoints:Set(40, false)
      			end
			end)
			if Scadawin:IsShown() then
				LabelUI.TTPoints:Set(40, Scadawin, -79, 3)
			end
			UpdatePos()
			
			if LabelUI.CallBack.exprepbar then
				tinsert(LabelUI.CallBack.exprepbar, UpdatePos)
			end
		end
 end 

Skadamod:SetScript("OnEvent", function(self, event, arg1)
 	if event == "ADDON_LOADED" and arg1 == "Skada" then
		Addbutton()
		Skadamod:UnregisterEvent("ADDON_LOADED")
	end 
end)
  
local function Init()
	_, _, _, enabled, _, reason = GetAddOnInfo("Skada")
	if reason == "MISSING" or not enabled then return end
	SaveVars = LabelUI.SaveVars	
	noshow = LabelUI.noshow
	if _G.Skada then
		Addbutton()
	else
		Skadamod:RegisterEvent("ADDON_LOADED")
	end
	
end

function Skadamod.InitMod(main, profilename)
	if not profile[profilename] then
		for i,v in pairs(profile) do
			if type(i) == "string" then
				profilename = i
				break
			end
		end
	end
	for i,v in pairs(profile[profilename].skada) do
		cfg[i] = v		
	end
	LabelUI = main
	main:AddInit(Init, "Addon SkadaMod")
end 
  
_G.LabelUI_mod["Skadamod"] = Skadamod